import compressPlugin from 'vite-plugin-compression';

export const configCompressPlugin = (env) => {
  const {
    VITE_APP_BUILD_COMPRESS: compress,
    VITE_APP_BUILD_COMPRESS_DELETE_ORIGIN_FILE: deleteOriginFile,
  } = env;
  const compressList = compress.split(',');

  const plugins: any[] = [];

  if (compressList.includes('gzip')) {
    plugins.push(
      compressPlugin({
        ext: '.gz',
        algorithm: 'gzip', // 压缩算法,可选 [ 'gzip' , 'brotliCompress' ,'deflate' , 'deflateRaw']
        // filter:()=>{}, // 那些资源不压缩
        deleteOriginFile,
        threshold: 1024 * 500, // 体积大于 threshold 才会被压缩,单位 b
      }),
    );
  }
  if (compressList.includes('brotli')) {
    plugins.push(
      compressPlugin({
        ext: '.br',
        // filter:()=>{}, // 那些资源不压缩
        algorithm: 'brotliCompress',
        deleteOriginFile,
        threshold: 1024 * 500, // 体积大于 threshold 才会被压缩,单位 b
      }),
    );
  }
  return plugins;
};
